package cn.edu.wynu.control.loop;

public class Sequence {
    public static void main(String[] args) {
        // 等差数列1~1000,公差为1,求和
        int i = 1;
        int sum = 0;
        while (i <= 1000){
            sum += i;
            i++;
        }

        System.out.println("1~1000的和为: " + sum);

        //等差数列公式: Sn = n(a1 + an) / 2, 通过下面这种方式计算,性能更好
        int Sn = 1000 * (1 + 1000) / 2;
        System.out.println("1~1000的和为: " + Sn);
    }
}
